import pygame

from constants.Constants import *
from role.Missile import Missile
from role.Role import Role


class Tank(Role):

    def __init__(self, screen, image_name, direction=UP, top=0, left=0, live=True, stop=False, speed=10):
        self.images = {
            UP: pygame.image.load("resources/" + image_name + "U.gif"),
            DOWN: pygame.image.load("resources/" + image_name + "D.gif"),
            LEFT: pygame.image.load("resources/" + image_name + "L.gif"),
            RIGHT: pygame.image.load("resources/" + image_name + "R.gif")
        }
        super().__init__(screen, self.images[direction], direction=direction, top=top, left=left, live=live, stop=stop,
                         speed=speed)
        self.missiles = []
        self.explode = None

    def play(self):
        if self.explode is not None:
            pygame.mixer.music.load("resources/blast.wav")
            pygame.mixer.music.play()

            for missile in self.missiles:
                self.missiles.remove(missile)
            return self.explode.play()
        else:
            self.image = self.images[self.direction]
            super().play()
            for missile in self.missiles:
                if missile.live:
                    missile.play()
                else:
                    self.missiles.remove(missile)

    def fire(self):
        if self.live:
            self.missiles.append(Missile(self))
